﻿Imports System.Data.OleDb
Public Class detail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub b_Comment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles b_Comment.Click


        Dim qs As String
        qs = "1"

        If Not (Request.QueryString("dealid") Is Nothing) Then
            qs = Request.QueryString("dealid")
        End If

        Dim sql As String = "INSERT INTO Comments (Author,Title,Commentbody,Deal_fkID, table_ID) VALUES (@f1,@f2,@f3,@f4,@f5)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("DealsDbConnection").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_title.Text)
        cmd.Parameters.AddWithValue("@f3", tb_cb.Text)
        cmd.Parameters.AddWithValue("@f4", qs)
        cmd.Parameters.AddWithValue("@f5", 1)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        'Response.Redirect("detail.aspx?dealid=" & qs)
        GridView1.DataBind()

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Adddealstodb.Click
        Dim sql As String = "INSERT INTO Dealtags (Deal_fkID,Tag_fkID) VALUES (@f1,@f2)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("DealsDbConnection").ConnectionString)
        conn.Open()
        For Each cb As ListItem In cb_tags.Items
            If cb.Selected = True Then
                Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", Request.QueryString("dealid"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()

            End If
        Next
    End Sub

    Protected Sub btn_newtag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_newtag.Click
        Dim sql As String = "INSERT INTO Tags (Tagname) VALUES (@f1)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("DealsDbConnection").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", tb_newtag.Text)
        cmd.ExecuteNonQuery()

        Dim cmd2 As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM TAGS ORDER BY ID DESC", conn)
        Dim dr As OleDbDataReader = cmd2.ExecuteReader()
        dr.Read()
        Dim highestId = dr(0)
        Dim associate As String = "INSERT INTO Dealtags (Deal_fkID,Tag_fkID) VALUES (@f1,@f2)"
        Dim cmd3 As OleDbCommand = New OleDbCommand(associate, conn)
        cmd3.Parameters.AddWithValue("@f1", Request.QueryString("dealid"))
        cmd3.Parameters.AddWithValue("@f2", highestId)
        cmd3.ExecuteNonQuery()

        cb_tags.DataBind()
    End Sub
End Class